﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace Karasoft.Common
{
    public static class Common
    {
        public static bool SendEmail(MailMessage omessage, SmtpClient osc)
        {
            try
            {
                osc.Send(omessage);
                return true;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        public static bool SendEmail(string subject, string body, string to, SmtpClient osc)
        {
            try
            {
                MailMessage omessage = new MailMessage();
                omessage.Subject = subject;
                omessage.Body = body;
                omessage.To.Add(to);
                osc.Send(omessage);
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        //public static Guid? AddNewMemberShipUser(string username, string password, string role)
        //{

        //    return AddNewMemberShipUser(username, password, role, "/");
        //}
        public static Guid? AddNewMemberShipUser(string username, string password, string role, string ProviderName)
        {
            string[] rolesf = { role };
            return AddNewMemberShipUser(username, password, rolesf, ProviderName);
        }
        //public static Guid? AddNewMemberShipUser(string username, string password, string[] roles)
        //{

        //    return AddNewMemberShipUser(username, password, roles, "/");
        //}
        public static Guid? AddNewMemberShipUser(string username, string password, string[] roles, string ProviderName)
        {
            System.Web.Security.MembershipCreateStatus omcs;
            System.Web.Security.MembershipProvider oprov = System.Web.Security.Membership.Providers[ProviderName];
            System.Web.Security.RoleProvider orp = System.Web.Security.Roles.Providers[ProviderName];

            //System.Web.Security.Membership.ApplicationName = AppName;
            //  System.Web.Security.Roles.ApplicationName = AppName;

            System.Web.Security.MembershipUser ouser = oprov.GetUser(username, false);
            if (ouser == null)
            {
                ouser = oprov.CreateUser(username, password, string.Empty, "Karasoft", "Karasoft", true, null, out omcs);
                foreach (var item in roles)
                {
                    if (!orp.RoleExists(item))
                        orp.CreateRole(item);
                    if (!orp.IsUserInRole(username, item))
                    {
                        string[] rolesarray = { item };
                        string[] userarray = { username };
                        orp.AddUsersToRoles(userarray, rolesarray);
                    }

                }
            }
            else
            {
                return new Guid(ouser.ProviderUserKey.ToString()); ;
            }

            return new Guid(ouser.ProviderUserKey.ToString());
        }

        public static Guid? AddNewDefaultMembershipUser(string username, string password, string role, string ProviderName)
        {
            string[] rolesf = { role };
            return AddNewDefaultMembershipUser(username, password, rolesf, ProviderName);
        }

        public static Guid? AddNewDefaultMembershipUser(string username, string password, string[] roles, string ProviderName)
        {
            System.Web.Security.MembershipCreateStatus omcs;
            System.Web.Providers.DefaultMembershipProvider oprov = new System.Web.Providers.DefaultMembershipProvider();
           // oprov.
            System.Web.Providers.DefaultRoleProvider orp = new System.Web.Providers.DefaultRoleProvider();

            //System.Web.Security.Membership.ApplicationName = AppName;
            //  System.Web.Security.Roles.ApplicationName = AppName;

            System.Web.Security.MembershipUser ouser = oprov.GetUser(username, false);
            if (ouser == null)
            {
                ouser = oprov.CreateUser(username, password, string.Empty, "Karasoft", "Karasoft", true, null, out omcs);
                foreach (var item in roles)
                {
                    if (!orp.RoleExists(item))
                        orp.CreateRole(item);
                    if (!orp.IsUserInRole(username, item))
                    {
                        string[] rolesarray = { item };
                        string[] userarray = { username };
                        orp.AddUsersToRoles(userarray, rolesarray);
                    }

                }
            }
            else
            {
                return new Guid(ouser.ProviderUserKey.ToString()); ;
            }

            return new Guid(ouser.ProviderUserKey.ToString());
        }
        /// <summary>  
        /// Generates the random password.  
        /// </summary>  
        /// <param name="passwordLength">Length of the password.</param>  
        /// <param name="type">The type of password needed.</param>  
        /// <returns></returns>  
        public static string GenerateRandomPassword(int passwordLength, PasswordType type)
        {
            const string allowedChars = "abcdefghijkmnopqrstuvwxyz";
            const string allowedOnlyNumbers = "0123456789";
            const string allowedCharsWithCaps = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ";
            const string allowedCharsWithCapsAndNumbers = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789";
            const string allowedCharsWithCapsAndNumbersAndSymbols = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789!@$?_-";

            char[] chars = new char[passwordLength];
            Random rd = new Random();
            string passwordCombinations;

            switch (type)
            {
                case PasswordType.allowedChars:
                    passwordCombinations = allowedChars;
                    break;
                case PasswordType.allowedCharsWithCaps:
                    passwordCombinations = allowedCharsWithCaps;
                    break;
                case PasswordType.allowedCharsWithCapsAndNumbers:
                    passwordCombinations = allowedCharsWithCapsAndNumbers;
                    break;
                case PasswordType.allowedCharsWithCapsAndNumbersAndSymbols:
                    passwordCombinations = allowedCharsWithCapsAndNumbersAndSymbols;
                    break;
                case PasswordType.allowedOnlyNumbers:
                    passwordCombinations = allowedOnlyNumbers;
                    break;
                default:
                    passwordCombinations = allowedChars;
                    break;
            }

            for (int i = 0; i < passwordLength; i++)
            {
                chars[i] = passwordCombinations[rd.Next(0, passwordCombinations.Length - 1)];
            }

            //System.Web.Security.Membership.GeneratePassword(

            return new string(chars);
        }
    }

    public enum PasswordType
    {
        allowedChars,
        allowedCharsWithCaps,
        allowedCharsWithCapsAndNumbers,
        allowedCharsWithCapsAndNumbersAndSymbols,
        allowedOnlyNumbers
    }
}
